<?php
/**
 * Display list of inquiries attached to the connected user
 * with status and todo
 */

use Fox\SalesDistribution\Sales;
use Fox\Dao\SqlFragment;
use Fox\Common\Customer;
use Fox\Html\Interval;
use Fox\Html\DropList;
use Fox\Html\Input;
use Fox\Html\Screen;
use Doctrine\DBAL\Connection;
use Fox\Generator;
use Fox\Html\AnchorObject;
use Fox\SalesDistribution\Sales\Inquiry\Status;
use Fox\SalesDistribution\Sales\Inquiry\Type;
use Fox\SalesDistribution\Sales\Partner\Role;
use Fox\SalesDistribution\Sales\Inquiry;

global $typeLabel,$statusLabel;
$finder = new Type();
$typeLabel = $finder->find(null,null,null,null,true);
$finder = new Status();
$statusLabel = $finder->find(null,null,null,null,true);
$start = date("Y")."-01-01";
$endDate= new DateTime();

$screen = new Screen('sInquiries','search');

$block = new Fox\Html\Block("bTodo","Filtres");
$block->add(new Interval(array("emission_date","start_date"),true,$start,$endDate->format("Y-m-d")))
		->saveInSession()
		->loadFromSession();
// Status
$block->add(new DropList("status",true))->options($statusLabel,
										Generator::useAttribute("statusid"),
										Generator::useAttribute("label"));
$block->item("status")->unsetOption(Inquiry::STATUS_NEW);
// Offer type
$finder = new Type();
$typeLabel = $finder->find();
$block->add(new DropList(array("inquiry_type","type"),true))->options($typeLabel,
										Generator::useAttribute("typeid"),
										Generator::useAttribute("label"));
$block->add(new Input("customername"));
//
$hierarchyTree = Xview::instance()->component("material","tree");

// Responsible Agency
$finder = new Customer();
$agFragments[] = new Fox\Dao\SqlFragment("type = ?",array(Customer::TYPE_OFFICE));
$agFragments[] = User::current()->translateSelectionLimits(array(User::LIMIT_AGENCY => "customerid"));
$limitedAgencies = $finder->find(Fox\Dao\SqlFragment::combine($agFragments),null,null,null,true);


$block->add(new Fox\Html\DropList\CheckBox("agency",null,null,null,null,null,Fox\Html\DropList\CheckBox::RMODE_VERTICAL))
				->nbOfItemByBlock(3)
				->options($limitedAgencies,Generator::useAttribute('customerid'),
								Generator::useAttribute('name1'));

// Operationnal Assignement
if(Fox\Authorization\Manager::check(User::AUTH_VIEW_ALL))
{
	$agencies = $finder->find(new Fox\Dao\SqlFragment("type = ?",array(Customer::TYPE_OFFICE)));

	$block->add(new Fox\Html\DropList\CheckBox("op_entity",null,null,null,null,null,Fox\Html\DropList\CheckBox::RMODE_VERTICAL)
					,2,"agency")
					->nbOfItemByBlock(3)
					->options($agencies,Generator::useAttribute('customerid'),
									Generator::useAttribute('name1'));
}
$block->add($hierarchyTree);
// Customer Name or Contact Search

$block->collapsed(true);

$screen->add($block);

$inputs = $screen->get();
// Filter the result base on inputs
$fragments[] = $block->item("start_date")->fragments();
$fragments[] = $block->item("type")->fragments("t.type");
$fragments[] = $block->item("status")->fragments();
if(!empty($inputs['customername']))
{
	$name1 = $inputs['customername'];
	$fragments[] = new Fox\Dao\SqlFragment("cus.name1 LIKE ? ",array("%".$name1."%"));
}
if(!empty($inputs['label']))
{
	$label = $inputs['label'];
	$fragments[] = new Fox\Dao\SqlFragment("it.label LIKE ? ",array("%".$label."%"));
}
// Manage
if(is_array($inputs['hierarchyTree']))
{
	foreach($inputs['hierarchyTree'] as $values)
	{
		if(!isset($values['label']))
			continue;
		$linkToParent[$values['id']] = $values['label'];
		if(isset($values['coche']) && $values['coche'])
		{
			if($values['level'] > 2) {
				$matetialIds[] = $values['id'];
			}
		}

	}
}
if(isset($matetialIds)) {
	$fragments[] = new SqlFragment("materialid IN (?)",array($matetialIds),array(Connection::PARAM_STR_ARRAY));
}

if(isset($idToCode))
{
	$where = null;
	foreach($idToCode as $codeValue)
	{
		if(empty($where)) {
			$where = "hierarchy LIKE ? ";
		} else {
			$where .= "OR hierarchy LIKE ? ";
		}
		$codes[] = "$codeValue%";
	}

	$fragments[] = new SqlFragment("( ".$where." )",$codes);
}
$joins = array(Inquiry::$JOIN_PARTNERS,Inquiry::$JOIN_SOLDTO,Inquiry::$JOIN_ITEMS);

if(Fox\Authorization\Manager::check(User::AUTH_VIEW_ALL))
{
	$fragments[] = $block->item("op_entity")->fragments("op.customerid");
	$joins[] = Inquiry::$JOIN_OP_ENTITIES;
}
$fragAg = $block->item("agency")->fragments("pa.customerid");
if(!$fragAg->isEmpty())
	$fragments[] = SqlFragment::combine(array($fragAg,new SqlFragment("pa.role = ?",array(Role::RESPONSIBLE_AGENCY))));

$finder = new Inquiry();
// Find my Offers
$inquiries = array();
$fragments[] = User::current()->inquiryFragments();
$inquiries = array();

$inquiries = $finder->find(Fox\Dao\SqlFragment::combine($fragments),
							array("start_date" => "DESC"),
						$joins,null,true);

/* @var $grid \Fox\Grid */
$grid = Xview::instance()->component("inquiry","result");
$grid->initialize($inquiries,true);
$grid->title(Msg(Messages::BUNDLE,Messages::NUMBER_OF_INQUIRIES,$grid->count()));
$grid->layout()->scrollVertical(true);
echo "<h1>".Msg(Sales::BUNDLE_NAME,92)."</h1><br>";
echo $screen->display();
echo "<br>".$grid->display();